package com.studyadmin.controller;


import com.studyadmin.entity.Student;
import com.studyadmin.entity.StudentVideo;
import com.studyadmin.entity.Video;
import com.studyadmin.service.VideoService;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * <p>
 * 章节视频内容表 前端控制器
 * </p>
 *
 * @author lyl
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/studyadmin/video")
@CrossOrigin
public class VideoController {
//AKIDYv2C5i37tXu0oLccKlwqUs0qPhhRt4xa    id
    //OUJsABvOaU8UaOvBAsIRItCvNZT6RZR2    key

    @Autowired
    VideoService videoService;

    /**
     * 根据章节ID查询章节内容
     * @param chaId 课程id
     * @return 结果
     */
    @GetMapping ("/chaptervideoContent")
    public JsonUtil chapterVideoContent(@RequestParam(value = "chaId") String chaId){
        System.out.println("章节内容id"+chaId);
        return videoService.chapterVideoContentSer(chaId);
    }

    /**
     * 视频观看情况上传
     * @param studentVideo 请求后端数据：学生id，视频id，章节id，视频总时长
     * @return 插入结果
     */
    @PostMapping("/chapterStudentVideo")
    public JsonUtil chapterStudentVideo(@RequestBody StudentVideo studentVideo){
        return videoService.chapterStudentVideoSer(studentVideo);
    }

    /**
     * 章节上传视频
     * @param video 视频
     * @return url
     */
    @RequestMapping("/uploadVideo")
    @ResponseBody
    public JsonUtil uploadImage(@RequestParam(name = "video") MultipartFile video){
        JsonUtil jsonUtil = new JsonUtil();
        String fileName = System.currentTimeMillis() + video.getOriginalFilename();//获取文件名
        File dir = new File("D:\\Tomcat\\apache-tomcat-9.0.46\\webapps\\examples");
        if (!dir.exists()){
            dir.mkdirs();
        }

        File targetFile = new File(dir, fileName);
        String url = "";
        try {
            video.transferTo(targetFile);
            url = "http://localhost:8089/examples/" + fileName;//记录图片url地址
            jsonUtil.setCode(0);
            jsonUtil.setData(url);
            jsonUtil.setMsg("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("上传失败");
        }
        return jsonUtil;
    }


    /**
     * 上传视频成功，数据插入到数据库
     * @param  videoUrl 视频地址
     *
     *                stuNum
     * @return 结果
     */
    @GetMapping("/updateVideo")
    public JsonUtil updateStuImgUrl(@RequestParam(name = "videoUrl") String  videoUrl,
                                    @RequestParam(name = "courseId") String  courseId,
                                    @RequestParam(name = "chaId") String  chaId) {
        Video video = new Video();
        video.setChaId(chaId);
        video.setCourseId(courseId);
        video.setVideoSourceUrl(videoUrl);
        return videoService.updateVideoUrlSer(video);
    }





}

